package com.player.thirdGroup.controller;


import com.player.thirdGroup.entity.Log;
import com.player.thirdGroup.service.LogService;
import com.player.thirdGroup.util.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


/**
 * @author VULCAN
 * @create 2019-12-28 7:49
 * @desc 日志
 **/
@Controller
public class LogController {
    @Autowired
    private LogService logService;

    //跳转列表页面
    @RequestMapping(value = "/loglisthtml")
    public String userlisthtml(){

        return "loglist";
    }

    //log列表
    @GetMapping("loglist")
    @ResponseBody
    public BootStrapPage<Log> deptunderlist(BootStrapTablePage page){

        Page<Log> all = logService.findAll(page);

        return new BootStrapPage<Log>(all.getContent(),all.getTotalElements());
    }
    //导出表格
    @GetMapping("daochu")
    @ResponseBody
    public void ExportExcel(HttpServletResponse response){
        //拿到数据
        List<Log> allLog = logService.findAllLog();
        //设置excel标题
        String[] title={"ID","操作IP","操作角色","操作时间","操作内容"};

        String fileName="日志.xls";

        String sheetName="日志表";
        //决定 行 列  1代表多少行 2 代表多少列
        String [][] content = new String[allLog.size()][5];
        for (int i = 0; i < allLog.size(); i++) {
            content[i] = new String[title.length];
            Log obj = allLog.get(i);
            content[i][0] = obj.getId();
            content[i][1] = obj.getName();
            content[i][2] = obj.getDate().toString();
            content[i][3] = obj.getContent(); // 下标 !
            content[i][4] = obj.getIp();
        }
        HSSFWorkbook workbook = ExcelUtil.getHSSFWorkbook(sheetName, title, content);
        SetResponseHeaderUtil.setResponseHeader(response,fileName);
        try {
            OutputStream os= response.getOutputStream();
            workbook.write(os);
            //释放资源
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
